package yur.wedding.modules.sys.service.impl;

import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import yur.wedding.common.annotation.DataFilter;
import yur.wedding.common.utils.Constant;
import yur.wedding.common.utils.Query;
import yur.wedding.modules.sys.dao.SysDeptDao;
import yur.wedding.modules.sys.entity.SysDeptEntity;
import yur.wedding.modules.sys.entity.SysRoleEntity;
import yur.wedding.modules.sys.service.SysDeptService;
import yur.wedding.utils.PageUtils;


@Service("sysDeptService")
public class SysDeptServiceImpl extends ServiceImpl<SysDeptDao, SysDeptEntity> implements SysDeptService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SysDeptEntity> page = this.page(
                new Query<SysDeptEntity>().getPage(params),
                new QueryWrapper<SysDeptEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 获取子部门
     * @param parentId  上级部门ID
     * @return
     */
    @Override
    public List<Long> queryDetpIdList(Long parentId) {

        return baseMapper.queryDetpIdList(parentId);
    }

    /**
     *  过滤数据用
     * @param deptId
     * @return
     */
    @Override
    public List<Long> getSubDeptIdList(Long deptId) {
        //部门及子部门ID列表
        List<Long> deptIdList=new ArrayList<>();

        //获取子部门 ID
        List<Long> subIdList=queryDetpIdList(deptId);
        //递归
        getDeptTreeList(subIdList, deptIdList);

        return deptIdList;
    }

    /**
     *  @DataFilter  过滤  null  为空的数据    查询所有部门
     * @param map
     * @return
     */
    @Override
    @DataFilter(subDept = true, user = false, tableAlias = "t1")
    public List<SysDeptEntity> queryList(HashMap<String, Object> map) {



        return baseMapper.queryList(map);
    }

    @Override
    public List<Long> queryListParentId(Long deptId) {

        return baseMapper.queryDetpIdList(deptId);
    }



    /**
     * 递归
     * @param subIdList
     * @param deptIdList
     */
    public void  getDeptTreeList(List<Long> subIdList, List<Long> deptIdList){
        for (Long deptid: subIdList){
            List<Long> list=queryDetpIdList(deptid);
                if (list.size()>0){
                    getDeptTreeList(list, deptIdList);
                }
                deptIdList.add(deptid);
        }
    }


    @Override
    public PageUtils queryDeptListPage(Map<String, Object> params) {


        String lastname = (String) params.get("lastname_s");
        QueryWrapper<SysDeptEntity> wrapper = new QueryWrapper<>();
        wrapper.like("name", lastname).or().like("pin_yin_dept_name", lastname);
        wrapper.apply(params.get(Constant.SQL_FILTER) != null, (String) params.get(Constant.SQL_FILTER));
        IPage<SysDeptEntity> page = this.page(
                new Query<SysDeptEntity>().getPage(params), wrapper
        );


        return new PageUtils(page);
    }

}
